package com.shuqi.model;

import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import android.content.Context;

import com.shuqi.common.MyTask;
import com.shuqi.common.Urls;
import com.shuqi.common.utils.HttpCommonParamsUtils;
import com.shuqi.common.utils.Log4an;
import com.shuqi.common.utils.MyUrlTools;
import com.shuqi.model.manager.MyAccountManager;
import com.shuqi.model.net.HttpRequest;
import com.shuqi.model.net.UrlManager;
import com.shuqi.model.net.listener.HttpRequestListener;
import com.shuqi.model.parse.parser.BookCoverParser;

/**
 * 书籍封面数据
 * 
 * @author yanghuawei
 * 
 */
public class BookCoverModel extends ModelBase implements HttpRequestListener {
    private final String logTag = "BookCoverModel";
    private HttpRequest httpRequest;
    private Context mContext;
    private String bookId;
    private String userId;

    public BookCoverModel(Context context) {
        this.mContext = context;
    }

    @Override
    public void action(int actionCode, Object object) {
        Log4an.e(logTag, "action:" + actionCode);
        fireEvent(actionCode, object);
    }

    @Override
    public List<BasicNameValuePair> setParams() {
        return HttpCommonParamsUtils.getShuQiBasicParams(userId);
    }

    @Override
    public String getRequestUrl() {
        return Urls.getBookCoverUrl(bookId, MyAccountManager.getInstance(mContext).getUserId());
    }

    @Override
    public void onDestroy() {
        if (httpRequest != null) {
            httpRequest.abort();
        }
    }

    @Override
    public void loadData(Object... objs) {
        Log4an.e(logTag, "loadData");
        bookId = (String) objs[0];
        userId = (String) objs[1];
        httpRequest =
                new HttpRequest(mContext, UrlManager.COLUMN_DOMAIN_READ, getRequestUrl(), setParams(), this);
        httpRequest.setDataParseAdapter(new BookCoverParser());
        MyTask.runInBackground(httpRequest, true);
    }
}
